home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-08-23 | 5.2 KB | 202 lines | [TEXT/ALFA] |
- #=============================================================================
- # "Electric" C functions.
- #=============================================================================
-
- # returns the indent string of the line named by 'pos'
- proc indentString pos {
- set start [lineStart $pos]
- set end [nextLineStart $pos]
- set text [getText $start $end]
- for {set i 0} {1} {incr i} {
- set c [string index $text $i]
- if {($c != "\ ") && ($c != "\t")} then {
- return [string range $text 0 [expr $i-1]]
- }
- }
- return
- }
-
-
- # Brace on new line, same indentation. Insert on another new line, indented in.
- # First, see if we are on new line.
- proc electricCLeft {} {
- global elecLBrace
- deleteText [getPos] [selEnd]
- if {$elecLBrace == "0"} then {
- insertText "\{"
- return
- }
- set pos [getPos]
- set start [lineStart $pos]
- set text [getText $start $pos]
-
- for {set i $start} {$i < $pos} {incr i} {
- set c [lookAt $i]
- if {($c != "\ ") && ($c != "\t")} then {
- set indentation [getText $start $i]
- insertText " \{\r" $indentation "\t"
- return
- }
- }
- set indentation [getText $start $pos]
- insertText "\{\r" $indentation "\t"
- }
- bind '\{' <s> electricCLeft C
- bind '\{' <s> electricCLeft C++
-
-
- # Brace on new line, immediate carriage return
- proc electricCRight {} {
- global elecRBrace
- deleteText [getPos] [selEnd]
- if {$elecRBrace == "0"} then {
- insertText "\}"
- catch {blink [matchIt "\}" [expr [getPos]-2]]}
- return
- }
- set pos [getPos]
- set start [lineStart $pos]
-
- if {[catch {matchIt "\}" [expr $pos-1]} matched]} {
- beep
- return
- }
- set text [getText [lineStart $matched] $matched]
- regexp {^[ ]*} $text indentation
- for {set i $start} {$i < $pos} {incr i} {
- set c [lookAt $i]
- if {($c != "\ ") && ($c != "\t")} then {
- insertText "\r" $indentation "\}\r" $indentation
- blink $matched
- return
- }
- }
- set text [set indentation]\}\r$indentation
- replaceText $start $pos $text
- goto [expr {$start + [string length $text]}]
- blink [matchIt "\}" [expr $start-2]]
- }
- bind '\}' <s> electricCRight C
- bind '\}' <s> electricCRight C++
-
-
- # Brace on new line, immediate carriage return. We don't do our electric stuff
- # if we are in the middle of a for statement.
- proc electricCSemi {} {
- global electricSemi
- deleteText [getPos] [selEnd]
- if {$electricSemi == "0"} then {
- insertText ";"
- return
- }
- set pos [getPos]
- set start [lineStart $pos]
- set text [getText $start $pos]
-
- if {[string first "for" $text] != "-1"} {
- set lefts 0
- set rights 0
- set len [string length $text]
- for {set i 0} {$i < $len} {incr i} {
- case [string index $text $i] in {
- "(" { incr lefts }
- ")" { incr rights }
- }
- }
- global globs
- set globs [list $lefts $rights $len]
- if {$lefts != $rights} {
- insertText ";"
- return
- }
- }
-
- insertText ";\r" [indentString $pos]
- }
- bind '\;' electricCSemi C
- bind '\;' electricCSemi C++
-
-
-
- #================================================================================
-
- proc CMarkFile {} {
- global CmodeVars
- set pos 0
- while {![catch {search -f 1 -r 1 -m 0 -i 0 $CmodeVars(funcExpr) $pos} res]} {
- set start [lindex $res 0]
- set end [expr [lindex $res 1] + 1]
- if {[regexp {([a-zA-Z0-9:_]+)[ \t]*\(} [getText $start $end] dummy word]} {
- set inds($word) [lineStart [expr $start - 1]]
- }
- set pos $end
- }
- if {[info exists inds]} {
- foreach f [lsort [array names inds]] {
- set next [nextLineStart $inds($f)]
- setNamedMark $f $inds($f) $next $next
- }
- }
- }
- proc CMarkFile {} {
- global CmodeVars
- set pos 0
- while {![catch {search -f 1 -r 1 -m 0 -i 0 $CmodeVars(funcExpr) $pos} res]} {
- set start [lindex $res 0]
- set end [expr [lindex $res 1] + 1]
- set text [getText $start $end]
- if {[regexp {([a-zA-Z0-9:_]+)[ \t]*\(} $text dummy word]} {
- set tmp [expr $start + [string first $word $text]]
- set inds($word) "$tmp [expr $tmp + [string length $word]]"
- }
- set pos $end
- }
- if {[info exists inds]} {
- foreach f [lsort -ignore [array names inds]] {
- set res $inds($f)
- setNamedMark $f [lineStart [lindex $res 0]] [lindex $res 0] [lindex $res 1]
- }
- }
- }
-
-
- #The previous version would not find things like void *ThisFunc( xxx ) due to the asterisk
- #I also truncated the pattern. The rest is not necessary and intrusive as far as I can tell
- proc C++MarkFile {} {
- set pos 0
- while {![catch {search -f 1 -r 1 -m 0 -i 0 {^([^ \t\(#\r/@].*[ \t]+)?\*?([A-Za-z0-9:~_]+)[ \t\r]*\(} $pos} res]} {
- set start [lindex $res 0]
- set end [expr [lindex $res 1] + 1]
- set thistext [getText $start $end]
- #regexp doesn't like carriage returns
- regsub -all "\r" $thistext " " thistext
- #regexp doesn't like tabs either
- regsub -all "\t" $thistext " " thistext
- #if the open paren was the last character on the line the selected text included the last carriage return as well
- #trim this off now that it is changed into a space
- set thistext [string trimright $thistext]
- if {[regexp {([a-zA-Z0-9:~_]+)[ \t]*\(} $thistext dummy word]} {
- set inds($word) [lineStart [expr $start - 1]]
- }
- set pos $end
- }
- if {[info exists inds]} {
- foreach f [lsort -ignore [array names inds]] {
- set next [nextLineStart $inds($f)]
- setNamedMark $f $inds($f) $next $next
- }
- }
- }
-
-
- proc setC++Mode {} {
- changeMode "C++"
- }
-
-
-
- source "$HOME:Tcl:SystemCode:think.tcl"
-
- proc dummyC {} {}
- proc dummyC++ {} {}
-